<?php

use Doraemon\model\AdmComplian;
use Doraemon\model\Comment;
use Doraemon\model\DecoCompanyCombo;
use Doraemon\model\DecoCompanyEvaluation;
use Doraemon\service\DingTalkAlert;
use Doraemon\service\ReMember\Member as sReMember;
use Doraemon\service\User;
use Doraemon\tools\Log;

class ReportAction extends ActionPlugin
{
    /**
     * hhzhome队列迁移 举报相关处理
     */
    public function main()
    {
        Log::info('举报-事件信息', $this->messageBody);
        $table = $this->messageBody['table'] ?? '';
        $aData = $this->messageBody['data'];

        if ($table !== 'hhz_adm_complain') {
            throw new \Exception('数据库错误！');
        }

        $info = AdmComplian::getById($aData['id']);
        if ($info && is_array($info)) {
            $user_info = (new sReMember)->getBaseMemberProfile($info['uid']);
            if ($info['from']) {
                $arr = explode(":", $info['from']);
                $dest = explode(":", $info['dest']);
                if ($arr[0] != 'uid') {
                    if ($dest[0] == 'cmt_id') {
                        $comment_row = Comment::getCommentById($arr[1], $dest[1]);
                        if ($comment_row && is_array($comment_row)) {
                            $BeComplained_uid = $comment_row['uid'];
                            $BeComplained = User::getUser($comment_row['uid']);
                            $beComplained_obj_id = $arr[1] . " 评论id" . $dest[1];
                        }
                    } else {
                        if ($arr[0] == 'deco_combo_id') {
                            $BeComplained_uid = DecoCompanyCombo::getUidByComboId($arr[1]);
                        } elseif ($arr[0] == 'deco_evaluate_id') {
                            $BeComplained_uid = DecoCompanyEvaluation::getUidById($arr[1]);
                        } else {
                            $BeComplained_uid = base_convert(substr($arr[1], -7), 36, 10);
                        }
                        $beComplained_obj_id = $arr[1];
                    }
                } else {
                    $BeComplained = $arr[1];
                    $beComplained_obj_id = $arr[1];
                }
                if ($BeComplained_uid) {
                    $BeComplained = User::getUser($BeComplained_uid);
                }
            }

            $content = "举报用户uid:" . $info['uid'] . " 被举报用户uid:" . $BeComplained_uid . "\n";
            $content .= "举报用户昵称:" . $user_info['nick'] . ' 被举报用户昵称:' . $BeComplained['nick'] . "\n";
            $content .= "举报类型:" . $this->type2name[$info['type']] . "\n";
            $content .= "举报内容id :" . $beComplained_obj_id . "\n";
            $content .= "举报内容:" . $info['message'] . "\n";

            DingTalkAlert::sendTextMsg(DingTalkAlert::USER_COMPLAINTS, $content);
        }
        $this->setSuccess();
    }

    //举报投诉
    private array $type2name = array(
        1 => 'note盗用',
        2 => '广告骚扰',
        3 => '人身攻击',
        4 => '政治敏感',
        5 => '侵权举报（诽谤、抄袭、冒用等）',
        6 => '违法信息（暴力恐怖、违禁品等）',
        21 => '功能需求',
        22 => '报告错误',
        23 => '选题建议',
        7 => '其它'
    );
}